package org.jeecg.modules.agCarGps.Job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.agCarGps.entity.AgCarGps;
import org.jeecg.modules.agCarGps.service.IAgCarGpsService;
import org.jeecg.modules.car.entity.CarInfo;
import org.jeecg.modules.car.service.ICarInfoService;
import org.jeecg.modules.cheldygx.entity.AgCheldygx;
import org.jeecg.modules.kuawcsFs.entity.AgKuawcsFs;
import org.jeecg.modules.kuawcsFs.service.IAgKuawcsFsService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.*;
import java.util.List;
@Slf4j
public class syncCarGps implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(String.format("同步V600，任务执行了，时间:" + DateUtils.formatDateTime()));
        long t1 = System.currentTimeMillis();
        syncCarGps();
        long t2 = System.currentTimeMillis();
        log.info(" 同步V600，任务执行结束了，执行时间:" + (t2-t1));    }

    @Autowired
    private IAgCarGpsService agCarGpsService;
    @Autowired
    private IAgKuawcsFsService agKuawcsFsService;
    @Autowired
    private ICarInfoService carInfoService;

    boolean yetFinishSync = false;

    @Value("${kwakcs.savePath}")
    String savePath;

    /*
    同步数据
     */
    void syncCarGps() {
//读取数据
        System.out.println("同步CarGps数据");
        List<AgCarGps> agCarGps = agCarGpsService.list(new QueryWrapper<AgCarGps>().eq("chuanswczt", "2"));


       // System.out.println("同步CarGps数据" + agCarGps);
        if (!agCarGps.isEmpty()) {
            JSONArray jsonArray = new JSONArray();

//            for (int i = 0; i < agCarGps.size(); i++) {
//
//                CarInfo carInfo = carInfoService.getOne(new QueryWrapper<CarInfo>().eq("imei", agCarGps.get(i).getImei()), false);
//                if (carInfo != null) {
//                    agCarGps.get(i).setCheph(carInfo.getId());
//                }
//                jsonArray.add(agCarGps.get(i));
//            }

            //写为文件
            BufferedWriter writer = null;
            //		String savePath="D:/Json测试";
            String fileName = "ag_car_gps" + ".json";
            String path = savePath + "/数据同步/" + fileName;
            File file = new File(path);
            String parentPath = file.getParent();
            File parentFile = new File(parentPath);
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            //如果文件不存在，则新建一个
            if (!file.exists()) {
                try {
                    file.createNewFile();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 格式化json字符串
            String jsonData = JSON.toJSONString(jsonArray);
            //写入
            try {
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));
                writer.write(jsonData);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (writer != null) {
                        writer.close();
                        System.out.println("文件生成成功！");

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("文件生成失败！");
                }
            }

            agCarGps.forEach(item -> {
                item.setChuanswczt("1");
                agCarGpsService.updateById(item);

            });

            //调用跨网传输
            AgKuawcsFs agKuawcsFs = new AgKuawcsFs();
            agKuawcsFs.setFasmbbm("anguan_erweima");
            agKuawcsFs.setFaswjm(fileName);
            agKuawcsFs.setFaswjlj(path);
            agKuawcsFs.setChuansfs("2");
            //  agKuawcsFsService.save(agKuawcsFs);
            agKuawcsFsService.sendFile(agKuawcsFs);
        }
    }

}
